<?php
session_start();
// 配置文件
define('TODO_FILE', 'todos.json');
define('UPLOAD_DIR', 'upload/');

// 初始化目录
if (!file_exists(UPLOAD_DIR)) {
    mkdir(UPLOAD_DIR, 0755, true);
}

// 读取现有数据
$todos = [];
if (file_exists(TODO_FILE)) {
    $todos = json_decode(file_get_contents(TODO_FILE), true) ?: [];
}

// 处理完成操作
if (isset($_GET['toggle'])) {
    $index = (int)$_GET['toggle'];
    if (isset($todos[$index])) {
        $originalStatus = $todos[$index]['status'];
        $todos[$index]['status'] = $originalStatus ? 0 : 1;
        // 当状态变为完成时设置待确认标记
        if (!$originalStatus && $todos[$index]['status'] == 1) {
            $todos[$index]['confirmed'] = 0;
        }
        file_put_contents(TODO_FILE, json_encode($todos, JSON_PRETTY_PRINT));
    }
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>我的作业</title>
    <?php session_start(); ?>
    <style>
        .completed { color: #999; text-decoration: line-through; }
        .todo-item { font: 2em sans-serif;;margin: 10px 0; padding: 5px; border-bottom: 1px solid #eee; }
        img.preview { max-width: 400px; vertical-align: middle; }
        .timer-btn { 
            padding: 12px 24px;
            font-size: 1em;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            display: block;
            margin-bottom: 25px;
        }
        .timer-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0,0,0,0.15);
        }
        .timer-btn:active {
            transform: translateY(0);
        }
        .button-container {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 25px;
        }
        a { 
            display: inline-block;
            margin-top: 15px;
            color: #333;
            text-decoration: none;
            font-size: 1.2em;
        }
    </style>
</head>
<body>
    <h2>我的作业</h2>
    <div class="button-container">
        <button onclick="location.href='add.php'" class="timer-btn">新增</button>
        <button onclick="location.href='https://home2025.infinityfreeapp.com/'" class="timer-btn">外网</button>
        <button onclick="location.href='http://192.168.50.5/'" class="timer-btn">内网</button>
        <button onclick="location.href='confirmed.php'" class="timer-btn">待确认</button>
        <button onclick="location.href='history.php'" class="timer-btn">已完成</button>
        <button onclick="location.href='edit.php'" class="timer-btn">重做</button>
        <button onclick="location.href='del.php'" class="timer-btn">删除</button>
        <button onclick="location.href='music.php'" class="timer-btn">播放音乐</button>
        <button onclick="location.href='timer.php'" class="timer-btn">学习计时</button>
    </div>
    <?php
// 获取当前日期
$today = date('Y-m-d');

// 仅显示未完成的作业
$filteredTodos = array_filter($todos, function($item) {
    return !$item['status'];
});

foreach (array_reverse($filteredTodos, true) as $index => $item): ?>
        <div class="todo-item <?= $item['status'] ? 'completed' : '' ?>">
            <small style="font-size: 0.5em;"><?= $item['date'] ?></small><br>
            <span><?= htmlspecialchars($item['content']) ?></span>
            <?php if (!$item['status']): ?>
                <a href="?toggle=<?= $index ?>" style="text-decoration:none;">
                    <img src="ok.jpg" style="width:30px;height:30px;vertical-align:middle;">
                </a>
            <?php endif; ?>
            <?php if (!empty($item['image']) && file_exists(UPLOAD_DIR . $item['image'])): ?>
                <br><a href="<?= UPLOAD_DIR . htmlspecialchars($item['image']) ?>" target="_blank">
                    <img src="<?= UPLOAD_DIR . htmlspecialchars($item['image']) ?>" 
                         class="preview">
                </a>
            <?php endif; ?>
        </div>
    <?php endforeach; ?>
</body>
</html>